﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GuavaData
{
    public class DropForeignKey : Operator
    {
        #region Fields
        string childTable;
        public string ChildTable
        {
            get { return childTable; }
            set { childTable = value; }
        }

        List<string> childConditionColumns;
        public List<string> ChildConditionColumns
        {
            get { return childConditionColumns; }
            set { childConditionColumns = value; }
        }

        List<Condition> childConditions;
        public List<Condition> ChildConditions
        {
            get { return childConditions; }
            set
            {
                // Dummy check - all conditions are equality conditions
                foreach (Condition c in value)
                    if (c.CondType != ConditionType.EQ)
                        throw new Exception("Foreign key conditions do not support conditions that are not equality conditions.");
                childConditions = value;
            }
        }

        List<string> childColumns;
        public List<string> ChildColumns
        {
            get { return childColumns; }
            set { childColumns = value; }
        }

        string parentTable;
        public string ParentTable
        {
            get { return parentTable; }
            set { parentTable = value; }
        }

        List<string> parentConditionColumns;
        public List<string> ParentConditionColumns
        {
            get { return parentConditionColumns; }
            set { parentConditionColumns = value; }
        }

        List<Condition> parentConditions;
        public List<Condition> ParentConditions
        {
            get { return parentConditions; }
            set
            {
                // Dummy check - all conditions are equality conditions
                foreach (Condition c in value)
                    if (c.CondType != ConditionType.EQ)
                        throw new Exception("Foreign key conditions do not support conditions that are not equality conditions.");
                parentConditions = value;
            }
        }

        List<string> parentColumns;
        public List<string> ParentColumns
        {
            get { return parentColumns; }
            set { parentColumns = value; }
        }
        #endregion Fields

        public DropForeignKey(string childTable, string parentTable)
        {
            this.parent = null;
            this.childTable = childTable;
            this.childConditionColumns = new List<string>();
            this.childConditions = new List<Condition>();
            this.childColumns = new List<string>();
            this.parentTable = parentTable;
            this.parentConditionColumns = new List<string>();
            this.parentConditions = new List<Condition>();
            this.parentColumns = new List<string>();
        }

        public DropForeignKey(string childTable, IEnumerable<string> childConditionColumns, IEnumerable<Condition> childConditions, IEnumerable<string> childColumns,
            string parentTable, IEnumerable<string> parentConditionColumns, IEnumerable<Condition> parentConditions, IEnumerable<string> parentColumns)
        {
            this.parent = null;
            this.childTable = childTable;
            this.childConditionColumns = new List<string>(childConditionColumns);
            this.childConditions = new List<Condition>(childConditions);
            this.childColumns = new List<string>(childColumns);
            this.parentTable = parentTable;
            this.parentConditionColumns = new List<string>(parentConditionColumns);
            this.parentConditions = new List<Condition>(parentConditions);
            this.parentColumns = new List<string>(parentColumns);

            // Three column checks
            if (this.childConditionColumns.Count != this.childConditions.Count)
                throw new Exception("Child condition column count does not match number of child conditions.");
            if (this.parentConditionColumns.Count != this.parentConditions.Count)
                throw new Exception("Parent condition column count does not match number of parent conditions.");
            if (this.childColumns.Count != this.parentColumns.Count)
                throw new Exception("Number of child columns does not match number of parent columns.");
        }

        public override void NotifyChange(Operator old, Operator newOp)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override bool isQuery
        {
            get
            {
                return false;
            }
        }
        public override SqlText Visit(Provider qv)
        {
            return qv.Transform(this);
        }
        public override void Visit(Transform t, Command c)
        {
            t.Expand(this, c);
        }
        public override Operator Clone()
        {
            return new DropForeignKey(this.childTable, this.childConditionColumns, this.childConditions, this.childColumns, this.parentTable, this.parentConditionColumns,
                this.parentConditions, this.parentColumns);
        }

        public void AddColumn(string childColumn, string parentColumn)
        {
            this.childColumns.Add(childColumn);
            this.parentColumns.Add(parentColumn);
        }

        public override void Visit(Channel c, string table)
        {
            c.MapAliases(this, table);
        }

    }
}
